package com.veryedu.hibernate.hbm.dao.impl;


import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.veryedu.hibernate.hbm.HibernateSessionFactory;
import com.veryedu.hibernate.hbm.dao.BaseDao;


public class BaseDaoImpl implements BaseDao{
	public Session session;
	public Transaction tr;
    /**  
     * ���ܣ������ݿ����ӵ�Session  
     * @return Session  
     */  
	public Session getSession() {
		this.session = HibernateSessionFactory.getSession();
		return session;
	}
	/**
	 * ���ܣ��ر�session
	 * @param session
	 */
	public void closeSession(Session session) {
		if (session != null && session.isOpen()) {
			session.close();
			session = null;
		}
	}
	/**
	 * ���ܣ�����µļ�¼
	 * @param entity  ӳ����
	 * @return    true  ��Ӽ�¼�ɹ�
	 *            false ��Ӽ�¼ʧ��
	 */
	public Object add(Object entity) {
		Session session = null;
		Transaction tr = null;
		boolean commitflag = false;
		try {
			session = this.getSession();
			tr = session.beginTransaction();
			entity = session.merge(entity);
			tr.commit();
			commitflag = true;
			session.flush();
			session.clear();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (!commitflag) {
				try {
					if (tr != null)
						tr.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
			this.closeSession(session);
		}
		return entity;
	}
	/**
	 * ����:ɾ���¼
	 * @param entity ӳ����
	 * @return  true ɾ��ɹ�
	 *          falseɾ��ʧ��
	 */
	public boolean delete(Object entity) {
		Session session = null;
		Transaction tr = null;
		boolean commitflag = false;
		try {
			session = this.getSession();
			tr = session.getTransaction();
			tr.begin();
			session.delete(entity);
			tr.commit();
			commitflag = true;
			session.flush();
			session.clear();
		} catch (Exception e) {
			if (tr != null)
				tr.rollback();
			System.out.println(" Class=" + entity.getClass().getName()+e);
			return false;
		} finally {
			if (!commitflag) {
				try {
					if (tr != null)
						tr.rollback();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			this.closeSession(session);
		}
		return true;
	}
	
	/**
	 * ͨ��int����ID ɾ��һ����ݿ��¼
	 * @param clazz  ӳ����.class
	 * @param id    PK ֵ
	 * @return      true  ɾ��ɹ�
	 *              false ɾ��ʧ��
	 */
	public boolean delete(Class clazz, int id) {
		try {
			Object entity = this.getByPk(clazz, id);
			if (entity != null) {
				this.delete(entity);
			} else {
				System.out.println(clazz.getName() + " �Ĺؼ���Ϊ " + id+ "  �Ķ��󲻴��� ");
				return false;
			}	
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * ɾ��ĳ������������
	 * @param clazz ʵ����.class
	 * @return      true  ɾ��ɹ�
	 *              false ɾ��ʧ��
	 */
	public boolean deleteTableData(Class entity) {
		Session session = null;
		Transaction tr = null;
		boolean commitflag = false;
		try {
			session = this.getSession();
			tr = session.beginTransaction();
			Query query = session.createQuery(" delete   from " + entity.getName());
			query.executeUpdate();
			tr.commit();
			commitflag = true;
			session.flush();
		} catch (Exception e) {
			System.out.println("����ݿ���ɾ��" + entity.getName() + "������ʵ��ʧ�ܣ�"+ e);
			return false;
		} finally {
			if (!commitflag) {
				try {
					if (tr != null)
						tr.rollback();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			this.closeSession(session);
		}
		return true;
	}
	/**
	 * ͨ��ʵ���༯�� ɾ����е��������
	 * @param  entities ʵ����ļ���(list vector Set��)
	 * @return true ɾ��ɹ� false ɾ��ʧ��
	 */
	public boolean deleteByEntities(Collection entities) {
		Session session = null;
		Transaction tr = null;
		boolean commitflag = false;
		try {
			session = this.getSession();
			tr = session.beginTransaction();
			Iterator ite = entities.iterator();
			while (ite.hasNext()) {
				session.delete(ite.next());
			}
			tr.commit();
			commitflag = true;
			session.flush();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (!commitflag) {
				try {
					if (tr != null)
						tr.rollback();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			this.closeSession(session);
		}
		return true;
	}
	
	/**
	 * ���ܣ�ͨ��PK��ȡ���  
	 * @param ʵ����.class
	 * @param id    PK
	 * @return      entity
	 */
	public Object getByPk(Class clazz, int id) {
		Object result = null;
		Session session = null;
		try {
			
			session = this.getSession();
			result = session.get(clazz, new Integer(id));
						
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeSession(session);
		}
		return result;
	}
	
	/**
	 * ���ܣ��������
	 * @param entity 
	 * @return  true false     
	 */
	public boolean update(Object entity) {
		Session session = null;
		Transaction tr = null;
		try {
			session = this.getSession();
			tr = session.beginTransaction();
			session.update(entity);
			tr.commit();
			session.flush();
			session.clear();
		} catch (Exception e) {
			e.printStackTrace();
			if (tr != null){
				tr.rollback();
			}
			return false;
		} finally {
			this.closeSession(session);
		}
		return true;
	}
	
	public List findAll(Class clazz) {
		Session session = null;
		List list = null;
		try {
			session = this.getSession();
			Query query = session.createQuery("from " + clazz.getName());
			list = query.list();
			session.flush();
			session.clear();
		} catch (Exception e) {
			e.printStackTrace();
			return list;
		} finally {
			this.closeSession(session);
		}
		return list;
	}
	
	public List findByProperty(Class clazz, String propertyName, Object value) {
		Session session = null;
		List list = null;
		try {
			session = this.getSession();
			String queryString = "from " + clazz.getName() + " as model where model."
					+ propertyName + "= ?";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, value);
			list =  queryObject.list();
			session.flush();
			session.clear();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeSession(session);
		}
		return list;
	}
}